/*
███████╗██╗   ██╗ ██████╗██╗  ██╗
██╔════╝██║   ██║██╔════╝██║ ██╔╝
█████╗  ██║   ██║██║     █████╔╝ 
██╔══╝  ██║   ██║██║     ██╔═██╗ 
██║     ╚██████╔╝╚██████╗██║  ██╗
╚═╝      ╚═════╝  ╚═════╝╚═╝  ╚═╝
*/
package dao;

import common.utils.DBUtilsV2;
import java.math.BigDecimal;
import java.sql.*;

public class OrderDao {

    /* 用户下单 */
    /* 用户下单 */
    public ResultSet createOrder(int userId, int productId, int qty, String address) throws SQLException {
        String sql = "{CALL sp_user_create_order(?, ?, ?, ?)}";   // 4 个参数
        Connection conn = DBUtilsV2.open();
        CallableStatement cstmt = conn.prepareCall(sql);
        cstmt.setInt(1, userId);
        cstmt.setInt(2, productId);
        cstmt.setInt(3, qty);
        cstmt.setString(4, address);
        cstmt.execute();
        return cstmt.getResultSet();
    }

    /* 管理员强制取消 */
    public void adminCancelOrder(int orderId) throws SQLException {
        String sql = "{CALL sp_admin_cancel_order(?)}";
        try (Connection conn = DBUtilsV2.open();
             CallableStatement cstmt = conn.prepareCall(sql)) {
            cstmt.setInt(1, orderId);
            cstmt.execute();
        }
    }
    /* 多模态查询订单 */
    public ResultSet queryOrders(Integer orderId, Integer userId, Integer productId,
                                 Integer status, java.sql.Timestamp start, java.sql.Timestamp end) throws SQLException {
        String sql = "{CALL sp_query_orders(?, ?, ?, ?, ?, ?)}";
        Connection conn = DBUtilsV2.open();
        CallableStatement cstmt = conn.prepareCall(sql);
        if (orderId == null) cstmt.setNull(1, Types.INTEGER); else cstmt.setInt(1, orderId);
        if (userId == null) cstmt.setNull(2, Types.INTEGER); else cstmt.setInt(2, userId);
        if (productId == null) cstmt.setNull(3, Types.INTEGER); else cstmt.setInt(3, productId);
        if (status == null) cstmt.setNull(4, Types.TINYINT); else cstmt.setInt(4, status);
        cstmt.setTimestamp(5, start);
        cstmt.setTimestamp(6, end);
        return cstmt.executeQuery(); // 调用者负责关闭
    }
}
